#dice.py
#Daniel Lovell (edited by Timothy Sassone)
#Contains objects and methods which provide
#dice-rolling services to other objects/methods.

import random
import math

class thrower(object):
    def throw_ex(self, num):
        dice = self.throw(num)

        hits = self.htest(dice)
        glitch = self.gtest(dice)
        crit = self.ctest(hits)
        
        return dice, hits, glitch, crit
    
    def throw(self, num): #Generates the dice throws
        dice = []
        x = 0
        while  x < num:
            roll = random.randint(1,6)
            dice.append(roll)
            x += 1
        
        return sorted(dice)

    def htest(self, dice): #Counts number of fives or sixes
        hits = 0
        num = len(dice)
        for throw in range(num):
        	if dice[throw] > 4: hits += 1
        return hits

    def gtest(self, dice): #Determines if glitch occurs
        glct = 0 #number of ones
        glit = False
        threh = 0 #glitch threshold
        num = len(dice)
        threh = int(math.ceil(num/2.0)) #Threshold is half total dice rounded up
        for throw in range(num):
            if dice[throw] == 1: glct += 1
        if glct >= threh:
            glit = True #If number of ones greater than threshold glitch occurs
        return glit

    def ctest(self, hits): #Determines if glitch critical
        crit = False
        if hits == 0: crit = True
        return crit
